#include<iostream>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>

int main()
{
    int sock=socket(AF_INET,SOCK_DGRAM,0);
    if(sock==-1)
    {
        std::cerr<<"Failed to create socket!"<<std::endl;
        return -1;
    }
    struct sockaddr_in server_addr;
    server_addr.sin_family=AF_INET;
    server_addr.sin_addr.s_addr=INADDR_ANY;
    server_addr.sin_port=htons(8080);
    if(bind(sock,(struct sockaddr*)&server_addr,sizeof(server_addr))<0)
    {
        std::cerr<<"Bind failed!"<<std::endl;
        return -1;
    }
    std::cout<<"UDP Server listening on port 8080..."<<std::endl;
    char buffer[1024]={0};
    struct sockaddr_in client_addr;
    socklen_t client_len=sizeof(client_addr);
    recvfrom(sock,buffer,1024,0,(struct sockaddr*)&client_addr,&client_len);
    std::cout<<"Client message:"<<buffer<<std::endl;
    const char* response="Hello from UDP server!";
    sendto(sock,response,strlen(response),0,(struct sockaddr*)&client_addr,client_len);
    close(sock);
    return 0;
}